package com.gusto.engine.spring.parsers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import com.gusto.engine.semsim.measures.impl.EdgeCountingSimilarity;

public class ParserEdgeCountingSimilarity extends AbstractBeanDefinitionParser {
	
	@SuppressWarnings("unchecked")
	protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
		BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(EdgeCountingSimilarity.class);
		
		String depth = "";
		if (element.hasAttribute("depth")) {
			depth = element.getAttribute("depth");
		}
		beanDefinitionBuilder.addPropertyValue("depth", depth);
		
		if (element.hasAttribute("stereotype")) {
			String stereotype = element.getAttribute("stereotype");
			beanDefinitionBuilder.addPropertyValue("stereotype", stereotype);
		}
		
		Element parents = DomUtils.getChildElementByTagName(element, "parents");
		if (parents != null) {
			List parentsElements = DomUtils.getChildElementsByTagName(parents, "parent");
			List<String> ents = new ArrayList<String>(); 
			for (int i = 0; i < parentsElements.size(); ++i) {
				Element childElement = (Element) parentsElements.get(i);
				ents.add(childElement.getTextContent());
			}
			beanDefinitionBuilder.addPropertyValue("parentProperties", ents);
		}
		
		Element stops = DomUtils.getChildElementByTagName(element, "stops");
		if (stops != null) {
			List stopsElements = DomUtils.getChildElementsByTagName(stops, "stop");
			List<String> ents = new ArrayList<String>(); 
			for (int i = 0; i < stopsElements.size(); ++i) {
				Element childElement = (Element) stopsElements.get(i);
				ents.add(childElement.getTextContent());
			}
			beanDefinitionBuilder.addPropertyValue("stopResources", ents);
		}
		
		return beanDefinitionBuilder.getBeanDefinition();
	}
	
	
}
